package com.ciwong.epaper.modules.pad.homeworkwidget.widget;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.DataProvider;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.HomeworkWidgetCheckState;
import com.ciwong.epaper.modules.pad.homeworkwidget.presenter.PersonalInfor;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews.HomeworkRemoteViewsDecorator;
import com.ciwong.libs.utils.CWLog;

/**
 * date :2018/9/5
 * author: lzh
 * description: 主要用于监听 亮屏息屏广播，开启定时任务
 * version:
 */

public class HomeworkwidgetmService extends Service {
    private String TAG="HomeworkwidgetmService";
    private HomeworkReceiver homeworkReceiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化 网络加载
        registerReceiver();
//        createTimer();
    }

    private void createTimer() {
        Intent autoRefreshIntent = new Intent(this, HomeworkWidgetProvider.class);
        autoRefreshIntent.setAction(AppWidgetConstants.ACTION_REFRESH_AUTO);
        PendingIntent pending = PendingIntent.getBroadcast(getBaseContext(), 0, autoRefreshIntent, 0);
        // 1*60秒更新一次
        final long duration = 1 * 10 * 1000;
        long nowMillis = System.currentTimeMillis();
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, nowMillis, duration, pending);
    }

    private void registerReceiver() {

        IntentFilter filter = new IntentFilter();


        // 屏幕灭屏广播
//        filter.addAction(Intent.ACTION_SCREEN_OFF);
        // 屏幕解锁广播
//        filter.addAction(Intent.ACTION_USER_PRESENT);
        // 屏幕亮屏广播
        filter.addAction(Intent.ACTION_SCREEN_ON);
        // 开机广播
        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
        // 切网络广播
        filter.addAction(AppWidgetConstants.ACTION_NET_CHANGET_CONNECT);
        // 本地数据加载成功
       /* filter.addAction(AppWidgetConstants.ACTION_LOAD_LOCAL_HOMEWORK_SUCCESS);
        //网络数据加载成功
        filter.addAction(AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_SUCCESS);
        //网络数据加载失败
        filter.addAction(AppWidgetConstants.ACTION_LOAD_NET_HOMEWORK_FAILED);
        //已经登陆成功
        filter.addAction(AppWidgetConstants.ACTION_HAS_LOGIN_SUCESS);
        //登陆成功(aap内部发送)
        filter.addAction(LoginToken.ACTION_LOGIN_SUCCESS);
        //退出登陆(aap内部发送)
        filter.addAction(LoginToken.ACTION_EXIT_LOGIN);
        //绑定手机成功(aap内部发送)
        filter.addAction(LoginToken.ACTION_BIND_PHONE_SUCCESS);
        //加入班级成功(aap内部发送)
        filter.addAction(EpaperConstant.BROADCAST_CLASS_CHANGE);
        //-检查到已经加入班级
        filter.addAction(AppWidgetConstants.ACTION_HAS_JOINED_CLASS);

        //接收到作业状态的改变广播(aap内部发送)
        filter.addAction(EpaperConstant.BROADCAST_HOME_WORK_STATUS_CHANGE_MSG);
        filter.addAction(EpaperConstant.BROADCAST_HOME_WORK_STATUS_INTERACTIVE_TUTORIAL_FINASH);

*/
        homeworkReceiver = new HomeworkReceiver();
        registerReceiver(homeworkReceiver, filter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        checkLoginAndJoinState(getApplicationContext());
        onUpdateRefreshData(getApplicationContext());
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(homeworkReceiver);
    }
    private void onUpdateRefreshData(Context context) {
        CWLog.d(TAG,"lzh onUpdateRefreshData");
        //首次进入刷新
        if (HomeworkWidgetCheckState.isLogin(context)){
            if (HomeworkWidgetCheckState.isBindPhone()) {
                PersonalInfor.getInstance(context).refreshPerInfor(true,true);
                if (HomeworkWidgetCheckState.isJoinClass())
                    DataProvider.loadLocalHomeworks(context);
            }
        }
    }

    /**
     * 检测登录与加入班级状态
     * @param context
     */
    private void checkLoginAndJoinState(Context context) {
        HomeworkRemoteViewsDecorator.getInstance().checkIsLoginAndJoinClassState(context);
    }
}
